using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;

namespace SheLife.Application.Services
{
    /// <summary>
    /// 习惯服务实现
    /// </summary>
    public class HabitService : IHabitService
    {
        private readonly IUnitOfWork _unitOfWork;

        public HabitService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取习惯
        /// </summary>
        /// <param name="id">习惯ID</param>
        /// <returns>习惯实体</returns>
        public async Task<Habit?> GetHabitByIdAsync(Guid id)
        {
            return await _unitOfWork.HabitRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 获取用户的所有习惯
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>习惯列表</returns>
        public async Task<IEnumerable<Habit>> GetHabitsByUserIdAsync(Guid userId)
        {
            var allHabits = await _unitOfWork.HabitRepository.GetAllAsync();
            return allHabits.Where(h => h.UserId == userId).OrderByDescending(h => h.CreatedOn);
        }

        /// <summary>
        /// 获取用户指定类型的习惯
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="category">习惯类型</param>
        /// <returns>习惯列表</returns>
        public async Task<IEnumerable<Habit>> GetHabitsByCategoryAsync(Guid userId, string category)
        {
            var allHabits = await _unitOfWork.HabitRepository.GetAllAsync();
            return allHabits.Where(h => h.UserId == userId && h.Category == category)
                           .OrderByDescending(h => h.CreatedOn);
        }

        /// <summary>
        /// 获取用户激活的习惯
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>习惯列表</returns>
        public async Task<IEnumerable<Habit>> GetActiveHabitsByUserIdAsync(Guid userId)
        {
            var allHabits = await _unitOfWork.HabitRepository.GetAllAsync();
            return allHabits.Where(h => h.UserId == userId && h.IsActive)
                           .OrderByDescending(h => h.CreatedOn);
        }

        /// <summary>
        /// 创建习惯
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="name">习惯名称</param>
        /// <param name="description">习惯描述</param>
        /// <param name="category">习惯类型</param>
        /// <param name="frequency">目标频率</param>
        /// <param name="targetCount">目标次数</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>习惯实体</returns>
        public async Task<Habit> CreateHabitAsync(Guid userId, string name, string description, string category, 
            string frequency = "daily", int targetCount = 1, DateTime? startDate = null, DateTime? endDate = null)
        {
            // 验证用户是否存在
            var user = await _unitOfWork.UserRepository.GetByIdAsync(userId);
            if (user == null || user.IsDeleted)
            {
                throw new ArgumentException("用户不存在");
            }

            // 创建习惯
            var habit = Habit.Create(userId, name, description, category, frequency, targetCount, startDate, endDate);

            await _unitOfWork.HabitRepository.AddAsync(habit);
            await _unitOfWork.SaveChangesAsync();

            return habit;
        }

        /// <summary>
        /// 更新习惯
        /// </summary>
        /// <param name="id">习惯ID</param>
        /// <param name="name">习惯名称</param>
        /// <param name="description">习惯描述</param>
        /// <param name="category">习惯类型</param>
        /// <param name="frequency">目标频率</param>
        /// <param name="targetCount">目标次数</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>习惯实体</returns>
        public async Task<Habit?> UpdateHabitAsync(Guid id, string name, string description, string category, 
    string frequency, int targetCount, DateTime? endDate = null)
{
    var habit = await _unitOfWork.HabitRepository.GetByIdAsync(id);
    if (habit == null)
    {
        return null;
    }

    habit.Update(name, description, category, frequency, targetCount, endDate);
    await _unitOfWork.HabitRepository.UpdateAsync(habit);
    await _unitOfWork.SaveChangesAsync();

    return habit;
}

        /// <summary>
        /// 激活/停用习惯
        /// </summary>
        /// <param name="id">习惯ID</param>
        /// <param name="isActive">是否激活</param>
        /// <returns>是否成功</returns>
        public async Task<bool> SetHabitActiveAsync(Guid id, bool isActive)
{
    var habit = await _unitOfWork.HabitRepository.GetByIdAsync(id);
    if (habit == null)
    {
        return false;
    }

    habit.SetActive(isActive);
    await _unitOfWork.HabitRepository.UpdateAsync(habit);
    await _unitOfWork.SaveChangesAsync();

    return true;
}

        /// <summary>
        /// 删除习惯
        /// </summary>
        /// <param name="id">习惯ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteHabitAsync(Guid id)
{
    var habit = await _unitOfWork.HabitRepository.GetByIdAsync(id);
    if (habit == null)
    {
        return false;
    }

    await _unitOfWork.HabitRepository.DeleteAsync(id);
    await _unitOfWork.SaveChangesAsync();

    return true;
}
    }
}